(function(context){
  var pluginsPath=basepath+'/plugins/v4',
  error=false,
  htmls=[];
  if(typeof(code)!=='undefined'&&code!==''&&code!==null)
	  error=true;
  if(!error){
	  if(show==='off'){//显示“电话咨询”按钮
		  htmls.push('<div class="free_ask" style="cursor:pointer; width:100px; text-align:center;  color:#fff; line-height:30px; height:30px; background:#38c4a9;">电话咨询</div>'); 
	  }
	  htmls.push('<div class="free_search"><div class="htDiv"><span class="span_tel"><img src="'+pluginsPath+'/img/tel.png" width="37" height="37" id="pic"></span><span class="span_theme">'+phones+'</span>');
	  if(show==='off'){//显示“关闭图标”
		  htmls.push('<a href="javascript:void(0)" class="close_btn" id="close"></a>'); 
	  }
	  htmls.push('</div><div class="zx_nr"><div class="f_step1"><p>本通话使用电话回拨能力，<span style="color:#000;font-weight:bold;">完全免费</span>');
	  htmls.push('<br/>将对您的号码严格保密，请放心使用<br/><span style="color:#000;font-weight:bold;">您也可以电话拨打'+phones+'</span></p><form action="" id="returnCallFrom" name="returnCallFrom">');
	  htmls.push('<div class="inform_div"><dl>');
	  htmls.push('<dt><input name="mobile" id="mobile" type="text" class="i_txt" placeholder="请输入您的电话（请勿使用总机号）"  style="width:213px;"/></dt>');
	  htmls.push('</dl><dl><dt><input name="randCode" id="randCodeConsult" type="text" class="i_txt1" placeholder="请输入右侧验证码" />');
	  htmls.push('<img id="randCodeImgConsult" onclick="refreshRandCodeConsult()" class="check_code" src="'+basepath+'/keyCode" /><span class="codeError" style="display:none" id="codeError"></span>');
	  htmls.push('</dt></dl><a href="javascript:void(0)" class="btn_zixun" id="RealMianFeiZiXun">免费咨询</a></div></form></div><div class="f_step2" style="display:none">');
	  htmls.push('<p>正在呼叫，请注意接听来电</p><img src="'+pluginsPath+'/img/tel_ing1.jpg" width="182" height="47"><a class="btn_cancel mt54">关闭</a></div></div></div>');	  
  }

  function validateNumber(number) {
	  var checkResult = {
	    success : true,
	    msg : '号码校验通过'
	  };
	  if(number===null||number===''){
	    checkResult.success = false;
	    checkResult.msg = '请输入电话号码';
	    return checkResult;
	  }
	  if (/[^0-9]/.test(number)) {// 存在非数字
	    checkResult.success = false;
	    checkResult.msg = '您输入的号码有误，请重新输入';
	    return checkResult;
	  }
	  var l = number.length;
	  if ((l === 7 || l === 8) && number.charAt(0) !== '0') {
	    checkResult.success = false;
	    checkResult.msg = '您输入的号码有误，若输入固话号码，请加区号';
	  } else if (l !== 11 && l !== 12) {
	    checkResult.success = false;
	    checkResult.msg = '您输入的号码有误，请重新输入';
	  }
	  return checkResult;
  }
  
  function loadScript(src){
	  var oHead = document.getElementsByTagName('head')[0]; 
	  var oScript= document.createElement("script"); 
	  oScript.type = "text/javascript"; 
	  oScript.src=src;
	  oHead.appendChild( oScript); 
  }
  
  context.$dudu={
	  create:function(id){
		  if(error){
			  alert('嘟嘟平台未授权当前用户使用点击拨号插件，因为['+describe+']');
			  return;
		  }
		  var context=$('#'+id)||$('.'+id);
		  if(context){
			  context.html(htmls.join(''));
			  setTimeout(function(){//所有元素加载完毕
				   loadScript(pluginsPath+'/js/ieplus.js');
					var error=$('.codeError'),p1=$('.f_step1'),p2=$('.f_step2'),sessionId=null;
				   if(show==='off'){
					    $('#close').css({
					    	background:'url('+pluginsPath+'/img/zx_close.png) 0 center no-repeat',
					    	backgroundSize:'14px 14px'
					    });
					    $('.free_ask').click(function(){
							$('.free_search').animate({left:'0px'},200).delay(200).animate({height:'305px'},200);
						});
					    $('.span_tel').css('cursor','pointer');
						$('.span_tel,.btn_cancel,.close_btn').click(function(){
							$('.free_search').animate({height:'305px'},200).delay(100).animate({left:'-352px'},200);
							p2.hide();
							p1.show();
							$("#randCodeConsult").val('');
							if(sessionId){
								$.ajax({
									type : "get",
									scriptCharset : "UTF-8",
									url : basepath+"/DIAL/dialStop.do", 
									data : {
										sessionid:sessionId,
									},
									dataType : "jsonp",
									complete:function(){
										sessionId=null;
									}								
								});
							}
						});
				   }else{
					   $('.free_search').animate({left:'0px'},200).delay(200).animate({height:'305px'},200);
					   $('.btn_cancel').click(function(){
							p2.hide();
							p1.show();
							$("#randCodeConsult").val('');
							$.ajax({
								type : "get",
								scriptCharset : "UTF-8",
								url : basepath+"/DIAL/dialStop.do",
								data : {
									sessionid:sessionId,
								},
								dataType : "jsonp"
							});
						});
				   }
					$(document.body).on('input','#mobile,#randCodeConsult',function(){
						var obj=$(this),value=obj.val();
						if(value===''){
							obj.css('color','rgb(200,200,200)');
						}else{
							obj.css('color','rgb(51,51,51)');
						}
					});
					$('.btn_zixun').click(function(){	
						var mobile=$('#mobile').val(),result=validateNumber(mobile);
						if(!result.success){
							error.show();
							error.text(result.msg);
							return;
						}
						var randCode=$("#randCodeConsult").val();
						if(randCode===''){
							error.show();
							error.text('请输入验证码');
							return;
						}
						$.ajax({
							type : "get",
							url : basepath+"/plugins/v4/createTwonessCommunicate.do",
							data:{randCode:randCode},
							dataType : "jsonp",
							scriptCharset : "UTF-8",
							contentType: "application/x-www-form-urlencoded; charset=UTF-8",
							success : function(data){
								if(data.code!='0'){
									error.show();
									error.text(data.describe);
								}else{
									$.ajax({
										dataType : "jsonp",
										scriptCharset : "UTF-8",
										contentType: "application/x-www-form-urlencoded; charset=UTF-8",
										type : "get",
										url : basepath+"/DIAL/dial.do",
										data : {
											caller:mobile,
											called:phones,
											app_key:app_key,
											cust_account:cust_account,
											ext_terminalCode:mobile,
											timestamp:timestamp,
											sign:sign,
											display_mode:''//显示配置号码
										},
										dataType : "jsonp",
										scriptCharset : "UTF-8",
										contentType: "application/x-www-form-urlencoded; charset=UTF-8",
										success : function(data) {
											if(data.result==='0'){
												error.hide();
												p1.hide();
												p2.show();
												sessionId=data.sessionId;
											}else{
												error.show();
												error.text(data.describe);
											}
										},
										complete : function(){
											refreshRandCodeConsult();
										}
									});
								}
							},
							complete : function(){
								refreshRandCodeConsult();
							}
							
						});
						
					});
					
			  },0);		  
		  }
	  }
  }
  
  context.refreshRandCodeConsult=function(){
	  //生成验证码
	  $('#randCodeImgConsult').hide().attr('src',basepath+'/keyCode?date='+new Date()).fadeIn();
  };
  
})(window);
 
	
